package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.dhc.danlu.R;

/**
 * 店铺类型
 */
public class StoreTypeSpiner extends Button implements OnItemClickListener {
    // ArrayList<String> list存储所要显示的数据
    private ArrayList<String> list;
    /**
     * 弹出框
     */
    private PopupWindow pop;
    /**
     * 获取父控件宽度
     */
    private RelativeLayout llytSpinner;

    public StoreTypeSpiner (Context context) {
        super (context);
    }

    public StoreTypeSpiner (Context context, AttributeSet attrs) {
        super (context, attrs);
    }

    public StoreTypeSpiner (Context context, AttributeSet attrs, int defStyle) {
        super (context, attrs, defStyle);
    }

    // 如果视图定义了OnClickListener监听器，调用此方法来执行
    @Override
    public boolean performClick () {
        Context context = getContext ();
        int[] location = new int[2];
        llytSpinner.getLocationOnScreen (location);
        final LayoutInflater inflater = LayoutInflater.from (getContext ());
        final View view = inflater.inflate (R.layout.layout_store_type_list, null);
        final ListView listview = (ListView) view
                .findViewById (R.id.store_type_list);
        CategorySpinnerListViewAdapter adapters = new CategorySpinnerListViewAdapter (context, getList ());
        listview.setSelector (new ColorDrawable (Color.TRANSPARENT));
        listview.setAdapter (adapters);
        listview.setOnItemClickListener (this);
        pop = new PopupWindow (view, llytSpinner.getWidth (),
                LayoutParams.WRAP_CONTENT, true);
        pop.setFocusable (true);
        pop.setBackgroundDrawable (new PaintDrawable ());
        // 指定PopupWindow显示在你指定的view下
        //pop.showAsDropDown(llytSpinner);
        pop.showAtLocation (llytSpinner, Gravity.TOP, location[0], location[1] - pop.getHeight ());

        return true;
    }

    @Override
    public void onItemClick (AdapterView<?> view, View itemView, int position,
                             long id) {
        setText (list.get (position));
        if (pop != null) {
            pop.dismiss ();
        }
    }

    public ArrayList<String> getList () {
        return list;
    }

    public void setList (ArrayList<String> list) {
        this.list = list;
    }


    public void setLinearLayout (RelativeLayout llytSpinner) {
        this.llytSpinner = llytSpinner;
    }

    public static class CategorySpinnerListViewAdapter extends BaseAdapter {
        private Context context;
        private ArrayList<String> list;

        public CategorySpinnerListViewAdapter (Context context, ArrayList<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount () {
            return list.size ();
        }

        @Override
        public Object getItem (int arg0) {
            return arg0;
        }

        @Override
        public long getItemId (int arg0) {
            return arg0;
        }

        @Override
        public View getView (int arg0, View arg1, ViewGroup arg2) {
            ViewHolder viewHolder = null;
            if (arg1 == null && list.size () != 0) {
                viewHolder = new ViewHolder ();
                LayoutInflater inflater = LayoutInflater.from (context);
                arg1 = inflater.inflate (R.layout.layout_store_type_item, null);
                viewHolder.textView = (TextView) arg1.findViewById (R.id.itemText);
                arg1.setTag (viewHolder);
            } else {
                viewHolder = (ViewHolder) arg1.getTag ();
            }
            viewHolder.textView.setText (list.get (arg0));
            return arg1;
        }

        public class ViewHolder {
            public TextView textView;
        }
    }
}